﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;
using System.Data;
using System.Workflow.Runtime;
using System.Workflow.Runtime.Hosting;
using System.Workflow.Activities;
using WebKpiPage.Data.GongdanMgr;
namespace WebKpiPage.BLL.GongdanMgr
{
    public class WFWorkHelper
    {
        public const string WorkflowRuntimeName = "WorkflowRuntime";

        public static string ConnectionString
        {
            get
            {
                return ConfigurationManager.ConnectionStrings["GZ_APP_ConStr"].ConnectionString;
            }
        }
        public WFWorkHelper()
        {
            //
            // TODO: 在此处添加构造函数逻辑
            //
        }
        public static Guid StartMyWorkflow(Type WorkflowType)
        {
            HttpApplicationState application = HttpContext.Current.Application;
            WorkflowRuntime workflowRuntime = application[WorkflowRuntimeName] as WorkflowRuntime;
            ManualWorkflowSchedulerService scheduler = workflowRuntime.GetService<ManualWorkflowSchedulerService>();
            WorkflowInstance workflowInstance = workflowRuntime.CreateWorkflow(WorkflowType);

            //启动
            workflowInstance.Start();
            //重新从持久化数据库中加载工作流，这是必须的，否则GetLoadedWorkflows无法获得新加入的流程。 
         //   workflowRuntime.GetWorkflow(workflowInstance.InstanceId); 

            if (scheduler.RunWorkflow(workflowInstance.InstanceId))
            {
                string state = (new StateMachineWorkflowInstance(workflowRuntime, workflowInstance.InstanceId)).CurrentStateName;
                System.Diagnostics.Trace.WriteLine(state);
                //开始运行
                return workflowInstance.InstanceId;
            }
            return Guid.Empty; ;

        }
        public static Guid WFActivityToPosition(Guid curentUserId)
        {
            DataSet ds = GongdanMgrCls.GetNextUser(curentUserId);
            try
            {
                if (ds != null && ds.Tables.Count > 0)
                {
                    return new Guid(ds.Tables[0].Rows[0]["ID"].ToString());
                }

            }
            catch (System.Exception ex)
            {

            }
            return Guid.Empty;
        }

        public static StateActivity GetCurrentState(Guid instanceId)
        {
            HttpApplicationState application = HttpContext.Current.Application;
            StateMachineWorkflowInstance stateInstance = new StateMachineWorkflowInstance(application[WorkflowRuntimeName] as WorkflowRuntime, instanceId);
            return stateInstance.CurrentState;
        }

        public static bool isWorkFlowBegin(string StateName, int WfType)
        {
            if (StateName.ToLower() == "beginstate")
                return true;
            else
                return false;
        }

        public static bool isWorkFlowEnd(string StateName, int WfType)
        {
            if (StateName.ToLower() == "closestate")
                return true;
            else
                return false;
        }


    }
}
